package cn.com.pxto.dao;

import java.util.HashMap;
import java.util.List;

import cn.com.pxto.commons.pagesupport.PageInfo;
import cn.com.pxto.dao.pagesupport.hibernate.HibernatePageDaoSupport;
import cn.com.pxto.model.User;


public class UserDAO extends HibernatePageDaoSupport {
	public User getUser(Integer uid){
		return (User) getTemplate().get(User.class, uid);
	}
	
	public void saveUser(User user){
		getTemplate().save(user);
	}
	
	public void deleteUser(Integer uid){
		String HQL="from User as u where u.uid="+uid;
		getTemplate().delete(HQL);
	}
	
	public void updateUser(User user){
		getTemplate().saveOrUpdate(user);
	}
	@SuppressWarnings("unchecked")
	public HashMap<String ,String> listUserForTag(){
		HashMap<String ,String> userMap=new HashMap();
		StringBuffer HQL=new StringBuffer();
		HQL.append("select u from User as u ");
		HQL.append(" order by u.uid");
		List<User> userList=getTemplate().find(HQL.toString());
		if (userList!=null && userList.size()>0) {
			for (int i = 0; i < userList.size(); i++) {
				User user=userList.get(i);
				userMap.put(user.getUid().toString(), user.getTrueName());
			}
		}
		return userMap;
	}
	public List<User> listUser(PageInfo pageInfo){
		StringBuffer HQL=new StringBuffer();
		HQL.append("select u from User as u ");
		HQL.append(" order by u.uid");
		return getTemplate().pagedFind(HQL.toString(), pageInfo);
	}
	/**
	 * return null if user not exist
	 * @param name
	 * @return
	 */
	public User getUser(String name){
		String HQL="select u from User as u where u.userName='"+name+"'";
		List list=getTemplate().find(HQL);
		if(list != null && list.size() > 0){
			return (User)list.get(0);
		}else{
			return null;
		}
	}
	public User getUserByTrueName(String trueName){
		String HQL="select u from User as u where u.trueName='"+trueName+"'";
		List list=getTemplate().find(HQL);
		if(list != null && list.size() > 0){
			return (User)list.get(0);
		}else{
			return null;
		}
	}
	
}
